// 操作数据库User表
import { Answer } from '../entity/Answer'
import { manager } from '../data-source'

// 保存答案
export const saveAnswer = async (data) => {
  const frequencyRepository = manager.getRepository('Frequency')
  const answerRepository = manager.getRepository('Answer')
  // 找到对应的场次
  const frequency = await frequencyRepository.findOne({ where: { id: data.frequencyId } })

  if (!frequency) {
    return await { message: '暂不可作答' }
  }
  // 创建新的 answer 实例
  const newPosts = data.answer.map((el) => {
    return answerRepository.create({
      ...el,
      frequency: frequency // 将 frequency 实例赋值给 answer 的 frequency 字段
    })
  })

  const res = await answerRepository.save(newPosts)

  return await true
}
